
#-*- coding:utf8 -*-
from datetime import datetime

from app import db

class User(db.Model):
    """
        usr 用户
    """
    __tablename__ = "usr"
    inr = db.Column(db.String(8), primary_key=True)
    nam = db.Column(db.String(20), unique=True)
    pwd = db.Column(db.String(100))
    dspnam = db.Column(db.String(40))
    eml = db.Column(db.String(200))
    tel = db.Column(db.String(20))
    credat = db.Column(db.DateTime)
    bchcod = db.Column(db.String(8))


    def __init__(self, inr, nam, pwd,
                 dspnam=None, eml=None, tel=None, credat=None, bchcod=None):
        self.inr = inr
        self.nam = nam
        self.pwd = pwd
        self.dspnam = dspnam
        self.eml = eml
        self.tel = tel
        if credat is None:
            credat = datetime.utcnow()
        self.credat = credat
        self.bchcod = bchcod

    def __repr__(self):
        return '<User %r(%s)>' % (self.nam, self.dspnam)

class  Tpa(db.Model):
    """
        tpa 考试
    """
    __table__name = "tpa"
    inr = db.Column(db.String(8), primary_key=True)
    dat = db.Column(db.DateTime)
    tle = db.Column(db.String(20))
    quesum = db.Column(db.Integer)
    exatim = db.Column(db.Integer)
    tpaflg = db.Column(db.String(1))
    usrinr = db.Column(db.String(8))
    typ = db.Column(db.String(8))
    rmk = db.Column(db.Text)

    def __init__(self, inr, tle, tpaflg, typ, quesum=0, exatim=0,
                 usrinr=None, dat=None, rmk=None):

        if dat is None:
            dat = datetime.utcnow()
        self.inr = inr
        self.tle = tle
        self.tpaflg = tpaflg
        self.typ = typ
        self.quesum = quesum
        self.exatim = exatim
        self.usrinr = usrinr
        self.dat = dat
        self.rmk = rmk

class Tql(db.Model):
    """
        tql 试卷试题
    """
    __tablename__ = "tql"
    inr = db.Column(db.String(8), primary_key=True)
    tpainr = db.Column(db.String(8))
    queinr = db.Column(db.String(8))

    def __init__(self, inr, tpainr, queinr):
        self.inr = inr
        self.tpainr = tpainr
        self.queinr = queinr

class Question(db.Model):
    """
        qus 题库
    """
    __tablename__ = "que"
    inr = db.Column(db.String(8), primary_key=True)
    ansexp = db.Column(db.String(500))
    answer = db.Column(db.String(300))
    answra = db.Column(db.String(300))
    answrb = db.Column(db.String(300))
    answrc = db.Column(db.String(300))
    answrd = db.Column(db.String(300))
    klgtyp = db.Column(db.String(12))
    que = db.Column(db.Text)
    quetyp = db.Column(db.String(12))
    degdif = db.Column(db.Float(8, 2))
    scr = db.Column(db.Integer)

    def __init__(self, inr, ansexp, answer, answra, answrb,
                 answrc, answrd, klgtyp, que, quetyp, degdif, scr):
        self.inr = inr
        self.ansexp = ansexp
        self.answer = answer
        self.answra = answra
        self.answrb = answrb
        self.answrc = answrc
        self.answrd = answrd
        self.klgtyp = klgtyp
        self.que = que
        self.quetyp = quetyp
        self.degdif = degdif
        self.scr = scr

class Kno(db.Model):
    """kno 知识点"""
    __tablename__ = "kno"
    inr = db.Column(db.String(8), primary_key=True)
    tpainr = db.Column(db.String(8))
    kwd = db.Column(db.String(12))
    kwdnum = db.Column(db.String(8))
    knolev = db.Column(db.String(1))
    quetyp = db.Column(db.String(12))

    def __init__(self, inr, tpainr, kwd, kwdnum, knolev, quetyp):
        self.inr = inr
        self.tpainr = tpainr
        self.kwd = kwd
        self.kwdnum = kwdnum
        self.knolev = knolev
        self.quetyp = quetyp

class Cnt(db.Model):
    """技术表"""
    __tablename__ = "cnt"
    nam = db.Column(db.String(40), primary_key=True)
    val = db.Column(db.Integer)
    stp = db.Column(db.Integer)

    def __init__(self, nam, val=1, stp=1):
        self.nam = nam
        self.val = val
        self.stp = stp

    def increase(self):
        self.val = self.val + self.stp

